#!/usr/bin/perl
#
# Show an automach entry
#
use Counter;
use Getopt::Long;
use MachMess;

GetOptions("unique" => \$dounique,
	   "message" => \$showmessage,
	   "computed" => \$showcomputed,
	   );

$key = shift;

$counter = Counter::open();
$dbh = FileTable::dbhandle();

if ($dounique) {
    $stm = $dbh->prepare("SELECT * from automach
          WHERE uniqueid = $key");
} else {
    $stm = $dbh->prepare("SELECT * from automach
          WHERE f_key = $key");
}
$count = $stm->execute();

warn "Found $count records\n";

while ($data = $stm->fetchrow_hashref()) {
    # Print out basic data
    print "Uniqueid  : ", $$data{uniqueid}, "\n";
    print "Name      : ", $$data{name}, "\n";
    print "Method    : ", $$data{method}, "\n";
    print "Updated   : ", $$data{updated}, "\n";
    # Print data from database
    print "Uptime    : ", $$data{uptime}, "\n";
    print "Max uptime: ", $$data{max_uptime}, "\n";
    print "CPU       : ", $$data{cpu}, "\n";
    print "Kernel    : ", $$data{kernel}, "\n";
    print "Bogomips  : ", $$data{bogomips}, "\n";
    # Print data computed from record, if requested
    if ($showcomputed) {
	print "-------------------- Computed ---------------------\n";
	$mess = MachMess->new($$data{lastmessage});
	print "Disk: ", $mess->disk(), "\n";
	print "Kernel(short): ", $mess->kernel("short"), "\n";
	print "Memory: ", $mess->memory(), "Mbytes\n";
    }
    # Print message
    if ($showmessage) {
	print "-------------------- Message ---------------------\n";
	print $$data{lastmessage};
    }
}
